﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    public class BLL_product
    { 
        /// <summary>
        /// 增
        /// </summary>
        /// <param name="pt"></param>
        /// <returns></returns>
        public string insert(product pt)
        {
            if (pt.product_refrences_user_email != null)
            {
                GodHelperDBDataContext da = new GodHelperDBDataContext();
                da.product.InsertOnSubmit(pt);
                da.SubmitChanges();
                string productid = "" + pt.product_id;
                return productid;
            }
            return null;
        }

        /// <summary>
        /// 删
        /// </summary>
        /// <param name="pt"></param>
        /// <returns></returns>
        public bool delete(product pt)
        {
            GodHelperDBDataContext da = new GodHelperDBDataContext();
            var p = da.product.Where(item => item.product_id == pt.product_id);
            if (p.Any())
            {
                da.product.DeleteOnSubmit(p.ToList()[0]);
                da.SubmitChanges();
                return true;
            }
            return false;
        }

        /// <summary>
        /// 改
        /// </summary>
        /// <param name="pt"></param>
        /// <returns></returns>
        public bool update(product pt)
        {
            GodHelperDBDataContext da = new GodHelperDBDataContext();
            var p = da.product.Where(item => item.product_id == pt.product_id);
            if (p.Any())
            {
                var var_product = p.ToList()[0];
                var_product.product_name = pt.product_name;
                var_product.product_sum = pt.product_sum;
                var_product.product_price = pt.product_price;
                var_product.product_decript = pt.product_decript;
                da.SubmitChanges();
                return true;
            }
            return false;
        }

        /// <summary>
        /// 查
        /// </summary>
        /// <param name="pt"></param>
        /// <param name="refrenceskey">根据那个键值查询</param>
        /// <returns></returns>
        public List<product> search(product pt, string refrenceskey)
        {
            GodHelperDBDataContext da = new GodHelperDBDataContext();
            switch (refrenceskey)
            {
                case "product_id":
                    if (pt.product_id != null)
                    {
                        var p = da.product.Where(item => item.product_id == pt.product_id);
                        if (p.Any())
                        {
                            return p.ToList();
                        }
                    }
                    break;
                case "product_refrences_user_email":
                    if (pt.product_refrences_user_email != null)
                    {
                        var p = da.product.Where(item => item.product_refrences_user_email == pt.product_refrences_user_email);
                        if (p.Any())
                        {
                            return p.ToList();
                        }
                    }
                    break;
            }
            return null;
        }
    }
}

